package com.gable.exhibition.service.impl;

import com.gable.common.util.ChineseToEnglish;
import com.gable.common.util.ReturnResultUtil;
import com.gable.exhibition.dao.*;
import com.gable.exhibition.model.*;
import com.gable.exhibition.service.BaseService;
import com.gable.exhibition.util.InsideOutsideUtil;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lesson on 2018/2/6.
 */
@Service("baseService")
public class BaseServiceImpl implements BaseService{

    @Autowired
    HospitalDao hospitalDao;

    @Autowired
    ManufacturerDao manufacturerDao;

    @Autowired
    MaintainerDao maintainerDao;

    @Autowired
    ProvinceDao provinceDao;

    @Autowired
    CityDao cityDao;

    @Autowired
    AreaDao areaDao;

    @Autowired
    ItemDao itemDao;

    @Autowired
    DeviceTypeDao deviceTypeDao;

    @Autowired
    DeviceTypeSubDao deviceTypeSubDao;

    @Autowired
    FileTypeDao fileTypeDao;

    @Autowired
    ManufacturerMaintainerDao manufacturerMaintainerDao;

    @Autowired
    SupplierDao supplierDao;

    @Autowired
    CommissionDao commissionDao;

    @Autowired
    PartDao partDao;

    @Autowired
    RepairDao repairDao;

    @Autowired
    UpkeepDao upkeepDao;

    @Autowired
    AppointDao appointDao;

    @Override
    public ReturnResultUtil queryHospitalBy(Map map) {
        List<Hospital> list = hospitalDao.queryHospitalListBySelect(map);
        for (Hospital hospital : list){
            hospital.setHospitalName(hospital.getHospitalName() + "[" + ChineseToEnglish.getPinYinHeadChar(hospital.getHospitalName()) + "]");
        }
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryManufacturer(Map map) {
        List<ManufacturerView> list = manufacturerDao.queryManufacturers(map);
        for (ManufacturerView manufacturer : list){
            manufacturer.setManufacturerName(manufacturer.getManufacturerName() + "[" + ChineseToEnglish.getPinYinHeadChar(manufacturer.getManufacturerName()) + "]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryMaintainer() {
        List<MaintainerView> list = maintainerDao.queryMaintainers();
        for (MaintainerView maintainer : list){
            maintainer.setMaintainerName(maintainer.getMaintainerName() + "[" + ChineseToEnglish.getPinYinHeadChar(maintainer.getMaintainerName()) + "]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryProvince() {
        List<Province> list = provinceDao.queryProvince();
        for (Province province : list){
            province.setProvince(province.getProvince() + "[" + ChineseToEnglish.getPinYinHeadChar(province.getProvince()) + "]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryCity(Long provinceId) {
        List<City> list = cityDao.queryCity(provinceId);
        for(City c : list){
            c.setCity(c.getCity() + "[" + ChineseToEnglish.getPinYinHeadChar(c.getCity())+"]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryArea(Map map) {
        List<Area> list = areaDao.queryArea(map);
        for (Area area : list){
            area.setArea(area.getArea() + "[" + ChineseToEnglish.getPinYinHeadChar(area.getArea())+"]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryItem(String type) {
        Map map = new HashMap();
        map.put("type",type);
        List<Item> list = itemDao.queryItem(map);
        for(Item item : list){
            item.setItem(item.getItem() + "[" + ChineseToEnglish.getPinYinHeadChar(item.getItem())+"]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryDeviceType() {
        List<DeviceType> list = deviceTypeDao.queryDeviceTypeListBySelect();
        for(DeviceType dt : list){
            dt.setName(dt.getName() + "[" +ChineseToEnglish.getPinYinHeadChar(dt.getName())+ "]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryDeviceTypeSub(Long parentId) {
        List<DeviceTypeSub> list = deviceTypeSubDao.queryDeviceTypeSubListBySelect(parentId);
        for(DeviceTypeSub dt : list){
            dt.setName(dt.getName() + "[" +ChineseToEnglish.getPinYinHeadChar(dt.getName())+ "]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryFileType(String fileType) {
        List<FileType> list = fileTypeDao.queryFileTypeListBySelect(fileType);
        for(FileType dt : list){
            dt.setFileTypeName(dt.getFileTypeName() + "[" +ChineseToEnglish.getPinYinHeadChar(dt.getFileTypeName()) +"]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    /**
     * 根据设备ID查询厂商设备信息
     * @param deviceId
     * @return
     */
    @Override
    public ReturnResultUtil queryManufacturerByDeviceId(Long deviceId){
        RepairManufacturerView repairManufacturerView = manufacturerDao.queryManufacturerByDeviceId(deviceId);
        String guaranteeStatus = InsideOutsideUtil.queryMedicalSecurity(repairManufacturerView.getEndDate());
        repairManufacturerView.setGuaranteeStatus(guaranteeStatus == null?"不清楚":guaranteeStatus);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,repairManufacturerView);
    }

    /**
     * 根据厂家ID查询维修商信息
     * @param manufacturerId
     * @return
     */
    public ReturnResultUtil getListByManufacturerId(Long manufacturerId){
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,manufacturerMaintainerDao.getListByManufacturerId(manufacturerId));
    }

    @Override
    public ReturnResultUtil queryPartList(PartView partView) {
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,partDao.queryPart(partView));
    }

    @Override
    public ReturnResultUtil queryMinListByMamuId(Long manufacturerId) {
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,manufacturerMaintainerDao.queryMinListByMamuId(manufacturerId));
    }

    @Override
    public ReturnResultUtil querySupplier(Map map) {
        List<Supplier> list = supplierDao.querySupplier(map);
        for(Supplier supplier : list){
            supplier.setShortName(supplier.getSupplierName() + "[" + ChineseToEnglish.getPinYinHeadChar(supplier.getSupplierName()) + "]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryCommission(Map map) {
        List<Commission> list = commissionDao.queryCommission(map);
        for(Commission c : list){
            c.setCommissionName(c.getCommissionName() + "[" + ChineseToEnglish.getPinYinHeadChar(c.getCommissionName()) + "]");
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil getMaintainerByHospitalId(ParameterView parameter) {
        if(parameter.getServiceType()==2901L){
            List<ParameterView> repair = repairDao.getMaintainerByHospitalId(parameter);
            return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,repair);
        }
        if(parameter.getServiceType()==2902L){
            List<ParameterView> upkeep = upkeepDao.getMaintainerByHospitalId(parameter);
            return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,upkeep);
        }
        return new ReturnResultUtil(200,MessageUtil.QUERYEXCEPTION);
    }

    @Override
    public ReturnResultUtil queryByRepairId(AppointView appoint) {
        List<AppointView> appoints = appointDao.queryByMaintainerName(appoint);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,appoints);
    }

    @Override
    public ReturnResultUtil queryMaintainerByCompletion(Map map) {
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,maintainerDao.queryMaintainerByCompletion(map));
    }

    @Override
    public ReturnResultUtil phoneCompletion(Map map) {
        List list = appointDao.phoneCompletion(map);
        return new ReturnResultUtil(200,MessageUtil.SUCCESSFUL,list);
    }
}
